let loopTimer;
let timer;
let onDrawing = false; // 是否可进行抽奖标识，默认为false可进行抽奖
let drawIndex = 0; //抽奖过程KEY
const moment = require('../../utils/moment.min');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 奖品参数信息
    prizeList: [{
        id: '001',
        index: 1,
        imgsrc: 'https://h5-touch.oss-cn-shanghai.aliyuncs.com/project/intel/image/prizes1.png',
        prizeName: '奖品1'
      },
      {
        id: '002',
        index: 2,
        imgsrc: 'https://h5-touch.oss-cn-shanghai.aliyuncs.com/project/intel/image/prizes2.png',
        prizeName: ' 奖品2'
      },
      {
        id: '003',
        index: 3,
        imgsrc: 'https://h5-touch.oss-cn-shanghai.aliyuncs.com/project/intel/image/no-prizes.png',
        prizeName: '谢谢参与'
      },
      {
        id: '004',
        index: 4,
        imgsrc: 'https://h5-touch.oss-cn-shanghai.aliyuncs.com/project/intel/image/prizes3.png',
        prizeName: '奖品3'
      },
      {
        id: '005',
        index: 5,
        imgsrc: 'https://h5-touch.oss-cn-shanghai.aliyuncs.com/project/intel/image/prizes1.png',
        prizeName: '奖品1'
      },
      {
        id: '006',
        index: 6,
        imgsrc: 'https://h5-touch.oss-cn-shanghai.aliyuncs.com/project/intel/image/no-prizes.png',
        prizeName: '谢谢参与'
      },
      {
        id: '007',
        index: 7,
        imgsrc: 'https://h5-touch.oss-cn-shanghai.aliyuncs.com/project/intel/image/prizes2.png',
        prizeName: '奖品2'
      },
      {
        id: '008',
        index: 8,
        imgsrc: 'https://h5-touch.oss-cn-shanghai.aliyuncs.com/project/intel/image/no-prizes.png',
        prizeName: '谢谢参与'
      },
    ],

    drawIndex: null, //抽奖过程KEY
    prizeResult: null, //抽奖结果KEY
    prizeName: null, //抽奖结果KEY对应的奖品名称
    isShowLuck: false, // 是否显示奖品弹窗，默认false不显示
    showAgain: false, //是否抽奖后显示再抽一次按钮
    awardImage: '', // 弹窗展示的奖品照片
    awardName: '', // 弹窗展示的奖品名字
    isShowFrom: false, //控制显示的是提示框还是表单弹框
    isShowLottery:false,//是否显示抽奖页面活动在 10-19 16:30显示抽奖的页面
    isShowStart:false,//在10-19 13：30-16：30之间显示即将开始的按钮
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(moment().format('YYYY-MM-DD HH:mm:ss'))
    var startTime = '2021-10-19 13:30:00';
    var endTime = '2021-10-19 16:30:00';
    let isShowStart = moment(startTime).isBefore();  //false
    let isShowLottery = moment(endTime).isBefore();  //false
    this.setData({
      isShowStart:isShowStart,
      isShowLottery:isShowLottery
    })
  },
  //抽奖操作
  startDraw() {
    this.againBtn()
    if (onDrawing) {
      return;
    } else {
      onDrawing = true;
    }

    clearInterval(loopTimer);
    clearTimeout(timer)
    loopTimer = setInterval(() => {
      this.changePrize()
    }, 100);

    // 随机将品池
    let random = Math.floor(Math.random() * this.data.prizeList.length)

    let res = {
      stutus: 1,
      prizeResult: this.data.prizeList[random].index,
      prizeName: this.data.prizeList[random].prizeName,
    }
    if (res.stutus == 1) {
      timer = setTimeout(() => {
        clearInterval(loopTimer);
        loopTimer = setInterval(() => {
          this.changePrize()
        }, 200);

        timer = setTimeout(() => {
          clearInterval(loopTimer);
          loopTimer = setInterval(() => {
            this.changePrize()
          }, 280);

          timer = setTimeout(() => {
            clearInterval(loopTimer)
            loopTimer = setInterval(() => {
              this.changePrize()
            }, 380);

            timer = setTimeout(() => {
              this.setData({
                prizeResult: res.prizeResult,
                prizeName: res.prizeName,
              });
              console.log(this.data.prizeResult)
              if (this.data.prizeResult === 3 || this.data.prizeResult === 6 || this.data.prizeResult === 8) {
                this.setData({
                  isShowFrom: false
                })
              } else {
                this.setData({
                  isShowFrom: true
                })
              }
            }, 1300)
          }, 1400)
        }, 1500)
      }, 1600)
    }
  },

  //抽奖过程奖品切换
  changePrize() {
    drawIndex++;
    drawIndex = drawIndex > 8 ? 1 : drawIndex;

    this.setData({
      drawIndex: drawIndex
    });

    if (this.data.prizeResult == drawIndex) {
      clearInterval(loopTimer)
      clearTimeout(timer)
      let currentAward = this.data.prizeList[this.data.prizeResult - 1]
      this.setData({
        showAgain: true,
        isShowLuck: true,
        awardImage: currentAward.imgsrc,
        awardName: currentAward.prizeName
      });
    }
  },
  // 关闭奖品弹簧
  closePopup() {
    this.setData({
      isShowLuck: false
    })
  },
  //点击再抽一次按钮
  againBtn() {
    onDrawing = false;
    drawIndex = 0; //抽奖过程KEY

    this.setData({
      drawIndex: null, // 清空抽奖过程KEY
      prizeResult: null, // 清空抽奖结果KEY
      prizeName: null, // 清空抽奖结果KEY对应的奖品名称
      showAgain: false, // 是否抽奖后显示再抽一次按钮
      awardImage: '', // 清空奖品展示的图片
      awardName: '', // 清空奖品的展示名称
      isShowFrom: false,
    });
  },
  //表单提交
  formSubmit(e) {
    console.log(e.detail.value)
    let tipMessage = ''
    if(e.detail.value.userName===''){
      tipMessage='请填写收件人'
    } else if(e.detail.value.phone==='') {
      tipMessage='请填写联系电话'
    } else if(e.detail.value.address===''){
      tipMessage='请填写收货地址'
    }
    if(e.detail.value.userName!==''&&e.detail.value.phone!==''&&e.detail.value.address!==''){
      this.setData({
        isShowFrom:false
      })
    } else {
      wx.showToast({
        title: tipMessage,
        icon: 'none',
        duration: 2000 //持续的时间
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
   
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },
})